#coding:utf-8
from .import auth 
from ..import db
from flask import render_template,request,url_for,redirect,session
from ..model import Users

#根据蓝本注册路由
@auth.route('/login')
#路由，装饰器的不是app，而是main
def login():
    return render_template('login.html')

@auth.route('/register')
def register():

    return render_template('register.html')

@auth.route('/commit_register' ,methods=['POST'])
def commit_register():
    name = request.form['name']
    psw = request.form['psw']
    passw = request.form['passw']
    
    print '>>>>>>>>',name,psw,passw
    user =db.session.query(Users).filter(Users.name==name).first()
    print '>>>>>>>>>',user
    #判断用户不存在，同事密码不存在
    #正则表达式(赋值给input的pattern属性)
    if not user and psw==passw:
        current_user =Users(name=name ,password=psw)
        db.session.add(current_user)
        db.session.commit()
    # return redirect('login')
    #同一个蓝本的话不用加文件夹名，非当前蓝本的加文件名(auth),url_for相当于动态加载链接
    return redirect(url_for('auth.login'))

@auth.route('/commit_login',methods=['POST'] )
def commit_login():
    name =request.form['name']
    psw =request.form['psw']
    user =db.session.query(Users).filter(Users.name==name).first()
    if user and user.verify_psw(psw):
        #记录session
        session['islogin']=True
        return redirect("/course")
    return 'login failed'
